//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "FlatForward.h"
using namespace Cephei::QL::Termstructures::Yield;
#include <gen/QL/Times/Calendar.h>
#include <gen/QL/Times/DayCounter.h>
#include <gen/QL/Quote.h>
#include <gen/QL/InterestRate.h>
#include <gen/QL/Times/Period.h>
#include <gen/QL/Termstructures/YieldTermStructure.h>
using namespace Cephei::QL::Times;
using namespace Cephei::QL;
using namespace Cephei::QL::Termstructures;
#define HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Termstructures::Yield::CFlatForward::CFlatForward (UInt32 settlementDays, Cephei::QL::Times::ICalendar^ calendar, Double forward, Cephei::QL::Times::IDayCounter^ dayCounter, Microsoft::FSharp::Core::FSharpOption<QL::CompoundingEnum>^ compounding, Microsoft::FSharp::Core::FSharpOption<QL::Times::FrequencyEnum>^ frequency) : CYieldTermStructure(CFlatForward::typeid)
{
    CCalendar^ _Ccalendar;
    CDayCounter^ _CdayCounter;
    try
    {
#ifdef HANDLE
        _phFlatForward = NULL;
#endif
        QuantLib::Natural _settlementDays = (QuantLib::Natural)ValueHelper::Convert (settlementDays); //d
        _Ccalendar = safe_cast<CCalendar^> (calendar);
        _Ccalendar->Lock();
        QuantLib::Calendar& _calendar = static_cast<QuantLib::Calendar&> (_Ccalendar->GetReference ()); 
        QuantLib::Rate _forward = (QuantLib::Rate)ValueHelper::Convert (forward); //d
        _CdayCounter = safe_cast<CDayCounter^> (dayCounter);
        _CdayCounter->Lock();
        QuantLib::DayCounter& _dayCounter = static_cast<QuantLib::DayCounter&> (_CdayCounter->GetReference ()); 
        QuantLib::Compounding _compounding = 
            (Microsoft::FSharp::Core::FSharpOption<QL::CompoundingEnum>::IsSome::get (compounding) ? (QuantLib::Compounding)compounding->Value : QuantLib::Compounding::Continuous); //10
        QuantLib::Frequency _frequency = 
            (Microsoft::FSharp::Core::FSharpOption<QL::Times::FrequencyEnum>::IsSome::get (frequency) ? (QuantLib::Frequency)frequency->Value : QuantLib::Frequency::Annual); //10
        _ppFlatForward = new boost::shared_ptr<QuantLib::FlatForward> (new QuantLib::FlatForward ( _settlementDays,  _calendar,  _forward,  _dayCounter,  _compounding,  _frequency ));
        SetYieldTermStructure (boost::dynamic_pointer_cast<QuantLib::YieldTermStructure> (*_ppFlatForward));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_Ccalendar != nullptr) _Ccalendar->Unlock();
        if (_CdayCounter != nullptr) _CdayCounter->Unlock();
    }
}
Cephei::QL::Termstructures::Yield::CFlatForward::CFlatForward (UInt32 settlementDays, Cephei::QL::Times::ICalendar^ calendar, Cephei::QL::IQuote^ forward, Cephei::QL::Times::IDayCounter^ dayCounter, Microsoft::FSharp::Core::FSharpOption<QL::CompoundingEnum>^ compounding, Microsoft::FSharp::Core::FSharpOption<QL::Times::FrequencyEnum>^ frequency) : CYieldTermStructure(CFlatForward::typeid)
{
    CCalendar^ _Ccalendar;
    CQuote^ _Cforward;
    CDayCounter^ _CdayCounter;
    try
    {
#ifdef HANDLE
        _phFlatForward = NULL;
#endif
        QuantLib::Natural _settlementDays = (QuantLib::Natural)ValueHelper::Convert (settlementDays); //d
        _Ccalendar = safe_cast<CCalendar^> (calendar);
        _Ccalendar->Lock();
        QuantLib::Calendar& _calendar = static_cast<QuantLib::Calendar&> (_Ccalendar->GetReference ()); 
        _Cforward = safe_cast<CQuote^> (forward);
        _Cforward->Lock();
        Handle<QuantLib::Quote>& _forward = static_cast<Handle<QuantLib::Quote>&> (_Cforward->GetHandle ()); 
        _CdayCounter = safe_cast<CDayCounter^> (dayCounter);
        _CdayCounter->Lock();
        QuantLib::DayCounter& _dayCounter = static_cast<QuantLib::DayCounter&> (_CdayCounter->GetReference ()); 
        QuantLib::Compounding _compounding = 
            (Microsoft::FSharp::Core::FSharpOption<QL::CompoundingEnum>::IsSome::get (compounding) ? (QuantLib::Compounding)compounding->Value : QuantLib::Compounding::Continuous); //10
        QuantLib::Frequency _frequency = 
            (Microsoft::FSharp::Core::FSharpOption<QL::Times::FrequencyEnum>::IsSome::get (frequency) ? (QuantLib::Frequency)frequency->Value : QuantLib::Frequency::Annual); //10
        _ppFlatForward = new boost::shared_ptr<QuantLib::FlatForward> (new QuantLib::FlatForward ( _settlementDays,  _calendar,  _forward,  _dayCounter,  _compounding,  _frequency ));
        SetYieldTermStructure (boost::dynamic_pointer_cast<QuantLib::YieldTermStructure> (*_ppFlatForward));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_Ccalendar != nullptr) _Ccalendar->Unlock();
        if (_Cforward != nullptr) _Cforward->Unlock();
        if (_CdayCounter != nullptr) _CdayCounter->Unlock();
    }
}
Cephei::QL::Termstructures::Yield::CFlatForward::CFlatForward (DateTime referenceDate, Double forward, Cephei::QL::Times::IDayCounter^ dayCounter, Microsoft::FSharp::Core::FSharpOption<QL::CompoundingEnum>^ compounding, Microsoft::FSharp::Core::FSharpOption<QL::Times::FrequencyEnum>^ frequency) : CYieldTermStructure(CFlatForward::typeid)
{
    CDayCounter^ _CdayCounter;
    try
    {
#ifdef HANDLE
        _phFlatForward = NULL;
#endif
        QuantLib::Date _referenceDate = (QuantLib::Date)ValueHelper::Convert (referenceDate); //d
        QuantLib::Rate _forward = (QuantLib::Rate)ValueHelper::Convert (forward); //d
        _CdayCounter = safe_cast<CDayCounter^> (dayCounter);
        _CdayCounter->Lock();
        QuantLib::DayCounter& _dayCounter = static_cast<QuantLib::DayCounter&> (_CdayCounter->GetReference ()); 
        QuantLib::Compounding _compounding = 
            (Microsoft::FSharp::Core::FSharpOption<QL::CompoundingEnum>::IsSome::get (compounding) ? (QuantLib::Compounding)compounding->Value : QuantLib::Compounding::Continuous); //10
        QuantLib::Frequency _frequency = 
            (Microsoft::FSharp::Core::FSharpOption<QL::Times::FrequencyEnum>::IsSome::get (frequency) ? (QuantLib::Frequency)frequency->Value : QuantLib::Frequency::Annual); //10
        _ppFlatForward = new boost::shared_ptr<QuantLib::FlatForward> (new QuantLib::FlatForward ( _referenceDate,  _forward,  _dayCounter,  _compounding,  _frequency ));
        SetYieldTermStructure (boost::dynamic_pointer_cast<QuantLib::YieldTermStructure> (*_ppFlatForward));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_CdayCounter != nullptr) _CdayCounter->Unlock();
    }
}
Cephei::QL::Termstructures::Yield::CFlatForward::CFlatForward (DateTime referenceDate, Cephei::QL::IQuote^ forward, Cephei::QL::Times::IDayCounter^ dayCounter, Microsoft::FSharp::Core::FSharpOption<QL::CompoundingEnum>^ compounding, Microsoft::FSharp::Core::FSharpOption<QL::Times::FrequencyEnum>^ frequency) : CYieldTermStructure(CFlatForward::typeid)
{
    CQuote^ _Cforward;
    CDayCounter^ _CdayCounter;
    try
    {
#ifdef HANDLE
        _phFlatForward = NULL;
#endif
        QuantLib::Date _referenceDate = (QuantLib::Date)ValueHelper::Convert (referenceDate); //d
        _Cforward = safe_cast<CQuote^> (forward);
        _Cforward->Lock();
        Handle<QuantLib::Quote>& _forward = static_cast<Handle<QuantLib::Quote>&> (_Cforward->GetHandle ()); 
        _CdayCounter = safe_cast<CDayCounter^> (dayCounter);
        _CdayCounter->Lock();
        QuantLib::DayCounter& _dayCounter = static_cast<QuantLib::DayCounter&> (_CdayCounter->GetReference ()); 
        QuantLib::Compounding _compounding = 
            (Microsoft::FSharp::Core::FSharpOption<QL::CompoundingEnum>::IsSome::get (compounding) ? (QuantLib::Compounding)compounding->Value : QuantLib::Compounding::Continuous); //10
        QuantLib::Frequency _frequency = 
            (Microsoft::FSharp::Core::FSharpOption<QL::Times::FrequencyEnum>::IsSome::get (frequency) ? (QuantLib::Frequency)frequency->Value : QuantLib::Frequency::Annual); //10
        _ppFlatForward = new boost::shared_ptr<QuantLib::FlatForward> (new QuantLib::FlatForward ( _referenceDate,  _forward,  _dayCounter,  _compounding,  _frequency ));
        SetYieldTermStructure (boost::dynamic_pointer_cast<QuantLib::YieldTermStructure> (*_ppFlatForward));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cforward != nullptr) _Cforward->Unlock();
        if (_CdayCounter != nullptr) _CdayCounter->Unlock();
    }
}
Cephei::QL::Termstructures::Yield::CFlatForward::CFlatForward (boost::shared_ptr<QuantLib::FlatForward>& childNative, Object^ owner) : CYieldTermStructure(CFlatForward::typeid)
{
#ifdef HANDLE
	_phFlatForward = NULL;
#endif
	_ppFlatForward = &childNative;
    _ppYieldTermStructure = new boost::shared_ptr<QuantLib::YieldTermStructure> (boost::dynamic_pointer_cast<QuantLib::YieldTermStructure> (*_ppFlatForward));
}
Cephei::QL::Termstructures::Yield::CFlatForward::CFlatForward (QuantLib::FlatForward& childNative, Object^ owner) : CYieldTermStructure(CFlatForward::typeid)
{
#ifdef HANDLE
	_phFlatForward = NULL;
#endif
	_ppFlatForward = new boost::shared_ptr<QuantLib::FlatForward> (&childNative);
    _ppYieldTermStructure = new boost::shared_ptr<QuantLib::YieldTermStructure> (boost::dynamic_pointer_cast<QuantLib::YieldTermStructure> (*_ppFlatForward));
    _FlatForwardOwner = owner;
    _YieldTermStructureOwner = owner;
}

Cephei::QL::Termstructures::Yield::CFlatForward::CFlatForward (CFlatForward^ copy) : CYieldTermStructure(CFlatForward::typeid)
{
#ifdef HANDLE
	_phFlatForward = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppFlatForward = new boost::shared_ptr<QuantLib::FlatForward> (copy->GetShared());
        _ppYieldTermStructure = new boost::shared_ptr<QuantLib::YieldTermStructure> (boost::dynamic_pointer_cast<QuantLib::YieldTermStructure> (*_ppFlatForward));
    }
}
Cephei::QL::Termstructures::Yield::CFlatForward::CFlatForward (PLATFORM::Type^ t) : CYieldTermStructure(CFlatForward::typeid)
{
#ifdef HANDLE
	_phFlatForward = NULL;
#endif
	if (!t->IsSubclassOf(CFlatForward::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Termstructures::Yield::CFlatForward::CFlatForward (QuantLib::Handle<QuantLib::FlatForward>& childNative, Object^ owner)  : CYieldTermStructure(CFlatForward::typeid)
{
	_phFlatForward = &childNative;
	_ppFlatForward = &static_cast<boost::shared_ptr<QuantLib::FlatForward>>(childNative.currentLink());
    _ppYieldTermStructure = new boost::shared_ptr<QuantLib::YieldTermStructure> (boost::dynamic_pointer_cast<QuantLib::YieldTermStructure> (*_ppFlatForward));
    _FlatForwardOwner = owner;
}
Cephei::QL::Termstructures::Yield::CFlatForward::CFlatForward (QuantLib::Handle<QuantLib::FlatForward> childNative)  : CYieldTermStructure(CFlatForward::typeid)
{
	_phFlatForward = &childNative;
	_ppFlatForward = &static_cast<boost::shared_ptr<QuantLib::FlatForward>>(childNative.currentLink());
    _ppYieldTermStructure = new boost::shared_ptr<QuantLib::YieldTermStructure> (boost::dynamic_pointer_cast<QuantLib::YieldTermStructure> (*_ppFlatForward));
}
#endif
#ifdef STRUCT
Cephei::QL::Termstructures::Yield::CFlatForward::CFlatForward (QuantLib::FlatForward childNative)  : CYieldTermStructure(CFlatForward::typeid)
{
#ifdef HANDLE
	_phFlatForward = NULL;
#endif
	_ppFlatForward = new boost::shared_ptr<QuantLib::FlatForward> (new QuantLib::FlatForward (childNative));
    _ppYieldTermStructure = new boost::shared_ptr<QuantLib::YieldTermStructure> (boost::dynamic_pointer_cast<QuantLib::YieldTermStructure> (*_ppFlatForward));
}
#endif

Cephei::QL::Termstructures::Yield::CFlatForward::~CFlatForward ()
{
    if (_ppFlatForward != NULL)
    {
	    delete _ppFlatForward;
        _ppFlatForward = NULL;
    }
}
Cephei::QL::Termstructures::Yield::CFlatForward::!CFlatForward ()
{
    if (_ppFlatForward != NULL)
    {
	    delete _ppFlatForward;
    }
}
QuantLib::FlatForward& Cephei::QL::Termstructures::Yield::CFlatForward::GetReference ()
{
    if (_ppFlatForward == NULL) throw REFNEW NativeNullException ();
	return **_ppFlatForward;
}
boost::shared_ptr<QuantLib::FlatForward>& Cephei::QL::Termstructures::Yield::CFlatForward::GetShared ()
{
    if (_ppFlatForward == NULL) throw REFNEW NativeNullException ();
	return *_ppFlatForward;
}
QuantLib::FlatForward* Cephei::QL::Termstructures::Yield::CFlatForward::GetPointer ()
{
    if (_ppFlatForward == NULL) throw REFNEW NativeNullException ();
	return &**_ppFlatForward;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::FlatForward>& Cephei::QL::Termstructures::Yield::CFlatForward::GetHandle ()
{
	if (_phFlatForward == NULL)
	{
		_phFlatForward = new Handle<QuantLib::FlatForward> (*_ppFlatForward);
	}
	return *_phFlatForward;
}
#endif
bool Cephei::QL::Termstructures::Yield::CFlatForward::HasNative () 
{
	return (_ppFlatForward != NULL);
}

QL::CompoundingEnum Cephei::QL::Termstructures::Yield::CFlatForward::Compounding::get ()
{
    try
    {
    	QuantLib::Compounding _rv = (QuantLib::Compounding)(*_ppFlatForward)->compounding ( );   
        QL::CompoundingEnum _nrv = (QL::CompoundingEnum)_rv;
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
QL::Times::FrequencyEnum Cephei::QL::Termstructures::Yield::CFlatForward::CompoundingFrequency::get ()
{
    try
    {
    	QuantLib::Frequency _rv = (QuantLib::Frequency)(*_ppFlatForward)->compoundingFrequency ( );   
        QL::Times::FrequencyEnum _nrv = (QL::Times::FrequencyEnum)_rv;
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
DateTime Cephei::QL::Termstructures::Yield::CFlatForward::MaxDate::get ()
{
    try
    {
    	QuantLib::Date _rv = (QuantLib::Date)(*_ppFlatForward)->maxDate ( );   
        DateTime _nrv = (DateTime)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Termstructures::Yield::IFlatForward^ Cephei::QL::Termstructures::Yield::CFlatForward::Update::get ()
{
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
    	(*_ppFlatForward)->update ( );
    	return this;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Termstructures::Yield::IFlatForward^ Cephei::QL::Termstructures::Yield::CFlatForward_Factory::Create (UInt32 settlementDays, Cephei::QL::Times::ICalendar^ calendar, Double forward, Cephei::QL::Times::IDayCounter^ dayCounter, Microsoft::FSharp::Core::FSharpOption<QL::CompoundingEnum>^ compounding, Microsoft::FSharp::Core::FSharpOption<QL::Times::FrequencyEnum>^ frequency)
{
    return REFNEW CFlatForward ( settlementDays,  calendar,  forward,  dayCounter,  compounding,  frequency);
}
Cephei::QL::Termstructures::Yield::IFlatForward^ Cephei::QL::Termstructures::Yield::CFlatForward_Factory::Create (UInt32 settlementDays, Cephei::QL::Times::ICalendar^ calendar, Cephei::QL::IQuote^ forward, Cephei::QL::Times::IDayCounter^ dayCounter, Microsoft::FSharp::Core::FSharpOption<QL::CompoundingEnum>^ compounding, Microsoft::FSharp::Core::FSharpOption<QL::Times::FrequencyEnum>^ frequency)
{
    return REFNEW CFlatForward ( settlementDays,  calendar,  forward,  dayCounter,  compounding,  frequency);
}
Cephei::QL::Termstructures::Yield::IFlatForward^ Cephei::QL::Termstructures::Yield::CFlatForward_Factory::Create (DateTime referenceDate, Double forward, Cephei::QL::Times::IDayCounter^ dayCounter, Microsoft::FSharp::Core::FSharpOption<QL::CompoundingEnum>^ compounding, Microsoft::FSharp::Core::FSharpOption<QL::Times::FrequencyEnum>^ frequency)
{
    return REFNEW CFlatForward ( referenceDate,  forward,  dayCounter,  compounding,  frequency);
}
Cephei::QL::Termstructures::Yield::IFlatForward^ Cephei::QL::Termstructures::Yield::CFlatForward_Factory::Create (DateTime referenceDate, Cephei::QL::IQuote^ forward, Cephei::QL::Times::IDayCounter^ dayCounter, Microsoft::FSharp::Core::FSharpOption<QL::CompoundingEnum>^ compounding, Microsoft::FSharp::Core::FSharpOption<QL::Times::FrequencyEnum>^ frequency)
{
    return REFNEW CFlatForward ( referenceDate,  forward,  dayCounter,  compounding,  frequency);
}
